﻿using UnityEngine;
using System.Collections.Generic;
using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Collections;
using tools;

namespace fil{
	public class ConfigManager{
		
		private static ConfigManager insit;
		
		public bool IsDone = false;
		private int finishIndex = 0;
		private Dictionary<string,object> xmlDic = new Dictionary<string,object>();
		
		public static ConfigManager getInstance () {
			
			if( insit == null ){
				insit = new ConfigManager();

				insit.IsDone = true;
				insit.readXML ("Alert");//章节表
			}
			return insit;
		}
		
		private string readStreamingFile (string name)
		{     
			TextAsset textasset = (TextAsset)Resources.Load ("Config/"+name,typeof(TextAsset));
			return textasset.text;
		}
		
		private void readXML(string filepath){
			string Newpath;
			XmlDocument xmlloader = new XmlDocument ();
			if (Application.platform == RuntimePlatform.IPhonePlayer) {
				TextAsset textAsset = (TextAsset)Resources.Load("Config/" + filepath, typeof(TextAsset));
				xmlloader.Load(textAsset.text);
			} else if (Application.platform == RuntimePlatform.Android) {
				Newpath = Resources.Load ("Config/" + filepath).ToString ();
				xmlloader.LoadXml(Newpath);

			}else if (Application.platform == RuntimePlatform.WindowsPlayer) {
				Newpath = Resources.Load ("Config/" + filepath).ToString ();
				xmlloader.LoadXml(Newpath);

			} else {
				Newpath = Application.dataPath + "/Resources/Config/" + filepath + ".xml";
				xmlloader.Load(Newpath);
			}

			XmlNodeList list = xmlloader.GetElementsByTagName("rotation");
			Dictionary<string,string>[] dicArr = new Dictionary<string,string>[list.Count];
			
			int index = 0;
			foreach(XmlNode node in list)
			{
				XmlAttributeCollection ac = node.Attributes;
				Dictionary<string,string> dic = new Dictionary<string, string>();
				foreach(XmlAttribute att in ac)
				{
					dic.Add(att.Name, att.Value);
				}
				dicArr[index] = dic;
				index++;
			}
			
			if (filepath.IndexOf (".") != -1) {
				string fileName  = filepath.Substring(0,filepath.IndexOf ("."));
				xmlDic [fileName] = dicArr;
			} else {
				xmlDic [filepath] = dicArr;
			}
			

			
			//		int index = 0; 
			//		foreach(XmlElement xe in nodeList)
			//		{
			//			Dictionary<string,string> dic = new Dictionary<string,string >();
			//			foreach(XmlElement x1 in xe.ChildNodes)
			//			{
			//				dic.Add(x1.Name,x1.InnerText);
			//				//Debug.Log(x1.Name+","+x1.InnerText);
			//			}
			//			dicArr[index] = dic;
			//			index++;
			//		}
			
			
			
		}
		
		public Dictionary<string,string> readDictionaryForId(string fileName,string key , string Id){
			Dictionary<string,string>[] dicArr = xmlDic[fileName] as Dictionary<string,string>[];
			for (int i = 0; i < dicArr.Length; i++) {
				if (dicArr [i] [key] == Id) {
					return dicArr [i];
				}
			}
			return null;
		}
		
		public Dictionary<string,string>[] readForDictionaryArr(string fileName){
			return xmlDic [fileName] as Dictionary<string,string>[];
		}
		
		public void readlanguages(){
			
			string txt = readStreamingFile("Languages");
			
			string[] arr = txt.Split('\n');
			
			for (int t = 0; t < arr.Length; t++) {
				
				string[] propertyName = arr[t].Split(' ');
				Languages.getInstance().setData(propertyName[0],propertyName[1]);
				
				if( t == arr.Length){
					addIndex();
				}
			}
			
		}
		
		private void addIndex(){
			finishIndex++;
		}
		
		private void setDown(){
			IsDone = true;
			
		}
	}
}
